/**
 * 给定一个排序链表，删除所有含有重复数字的节点，只保留原始链表中 没有重复出现 的数字。
 * <p>
 * 示例 1:
 * <p>
 * 输入: 1->2->3->3->4->4->5
 * 输出: 1->2->5
 * 示例 2:
 * <p>
 * 输入: 1->1->1->2->3
 * 输出: 2->3
 */
class Solution {

    public static void main(String[] args) {
        /*ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        node1.next = node2;*/
        ListNode node3 = new ListNode(3);
        //node2.next = node3;
        ListNode node4 = new ListNode(3);
        node3.next = node4;
        /*ListNode node5 = new ListNode(4);
        node4.next = node5;
        ListNode node6 = new ListNode(4);
        node5.next = node6;
        ListNode node7 = new ListNode(5);
        node6.next = node7;*/
        ListNode node = deleteDuplicates(node3);
        System.out.println(node);
    }

    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null) return null;
        ListNode node = head.next, pre = head, prePre = null;
        // 是否删除上一个节点
        boolean deleteFlag = false;
        // 遍历
        while (node != null) {
            if (pre.val == node.val) {
                if (node.next == null) {
                    // 如果后面没有节点了，就直接清理
                    if (prePre != null) {
                        prePre.next = null;
                    } else {
                        head = null;
                    }
                    break;
                } else {
                    deleteFlag = true;
                    pre.next = node.next;
                }
            } else if (deleteFlag) {
                deleteFlag = false;
                if (prePre != null) {
                    prePre.next = node;
                } else {
                    head = node;
                }
                pre = node;
            } else {
                prePre = pre;
                pre = node;
            }
            node = node.next;
        }
        return head;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}